package com.example.actionsignin.mvp.view;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;

import com.example.actionsignin.R;
import com.example.actionsignin.adapter.AddImageAdapter;
import com.example.actionsignin.dialog.AddCameraDialogFragment;
import com.example.actionsignin.mvp.model.DescribeInfo;
import com.example.actionsignin.mvp.presenter.IImageListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 活动简介及添加图片
 */
public class ActionDescribeActivity extends AppCompatActivity implements
        AdapterView.OnItemClickListener,
        IImageListener,
        View.OnClickListener {

    private EditText mEditText;

    private GridView gridView;
    private AddImageAdapter mAdapter;

    private List<Map<String,Object>> mList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action_describe);

        initView();
    }

    private void initView() {
        findViewById(R.id.action_describe_left).setOnClickListener(this);
        findViewById(R.id.action_describe_save).setOnClickListener(this);

        mEditText = (EditText) findViewById(R.id.action_describe_edit);

        gridView = (GridView) findViewById(R.id.action_describe_grid);

        setFirstBitmap();
        mAdapter = new AddImageAdapter(this, mList);
        gridView.setAdapter(mAdapter);

        gridView.setOnItemClickListener(this);
    }

    private void setFirstBitmap()
    {
        Map<String ,Object> map = new HashMap<>();
        map.put("bitmap",true);

        mList.add(map);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.action_describe_left:
                ActionDescribeActivity.this.finish();
                break;
            case R.id.action_describe_save:
                Intent intent = new Intent(ActionDescribeActivity.this, ReleaseActivity.class);
                Bundle bundle = new Bundle();
                DescribeInfo describeInfo = setDescribe();
                bundle.putSerializable("describe", describeInfo);
                intent.putExtras(bundle);

                setResult(RESULT_OK, intent);
                finish();
                break;
        }
    }

    @Override
    public void setImagePath(String path) {

        Map<String,Object> map = new HashMap<>();
        map.put("path",path);

        mList.remove(mList.size()-1);
        mList.add(map);
        setFirstBitmap();
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position == mList.size() - 1) {
            AddCameraDialogFragment addCameraDialogFragment = new AddCameraDialogFragment();
            addCameraDialogFragment.show(getSupportFragmentManager(), "addCameraDialog");
        }else {
            Intent intent = new Intent(ActionDescribeActivity.this,LookPhotoActivity.class);
            intent.putExtra("path",String.valueOf(mList.get(position).get("path")));

            startActivity(intent);
        }
    }

    private DescribeInfo setDescribe()
    {
        DescribeInfo describeInfo = new DescribeInfo();

        String describe = mEditText.getText().toString();
        if (!describe.equals(""))
        {
            describeInfo.setDescribe(describe);
        }

        List<Map<String,Object>> list = new ArrayList<>();
        for (int i=0;i<mList.size()-1;++i)
        {
            String path = String.valueOf(mList.get(i).get("path"));
            Map<String,Object> map = new HashMap<>();
            map.put("path",path);

            list.add(map);
        }
        if (list.size() > 0)
        {
            describeInfo.setList_path(list);
        }

        return describeInfo;
    }
}
